
package com.sanpu.kxe.utils.animations;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

/**
 * Description :动画的工具类，提供位移，透明度，旋转类的动画
 */
public class AnimationForViewUtil {

    /**
     * 参数错误提示语
     */
    private static final String PROMPT = "没有传入需要移动的view!";

    private static final String TAG = AnimationForViewUtil.class.getName();

    private static  final String TRANSLATION_X = "translationX";

    private static  final String TRANSLATION_Y = "translationY";

    private static final String ALPHA = "alpha";

    private static  final String ROTATION = "rotation";


    /**
     * 同步竖直移动
     *
     * @param translateParams 需要移动的view
     */
    public static synchronized void translateXSyn(TranslateParams translateParams) {
        translateSyn(translateParams, "translationX");
    }

    /**
     * 同步竖直移动
     *
     * @param translateParams 需要移动的view
     */
    public synchronized void translateYSyn(TranslateParams translateParams) {
        translateSyn(translateParams, "translationY");
    }

    /**
     * 同步平移动画
     *
     * @param translateParams 动画参数
     * @param tranType        动画类型
     */
    private static synchronized void translateSyn(TranslateParams translateParams, String tranType) {
        float start = 0F;
        float end = 360F;
        View view = null;
        int duration = 1000;
        Animator.AnimatorListener animationListener = null;
        if (translateParams != null) {
            if (TextUtils.equals(TRANSLATION_Y, tranType)) {
                if (translateParams.getStartY() != start) {
                    start = translateParams.getStartY();
                }
                if (translateParams.getEndY() != end) {
                    end = translateParams.getEndY();
                }
            } else if (TextUtils.equals(TRANSLATION_X, tranType)) {
                if (translateParams.getStartX() != start) {
                    start = translateParams.getStartX();
                }
                if (translateParams.getEndY() != end) {
                    end = translateParams.getEndY();
                }
            }

            if (translateParams.getView() != null) {
                view = translateParams.getView();
            }
            if (translateParams.getTranslateAnimationListener() != null) {
                animationListener = translateParams.getTranslateAnimationListener();
            }
        }
        if (view != null) {
            PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat(tranType, start, end);
            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, p1);
            if (animationListener != null) {
                animator.addListener(animationListener);
            }
            animator.setDuration(duration).start();
        } else {
            Log.v(TAG, PROMPT);
        }
    }


    /**
     * 异步动画加载
     */
    public void translateXAsy(TranslateParams translateParams) {
        translateAsy(translateParams, TRANSLATION_X);
    }

    /**
     * 异步动画加载
     */
    public void translateYAsy(TranslateParams translateParams) {
        translateAsy(translateParams, TRANSLATION_Y);
    }

    /**
     * 异步平移动画
     *
     * @param translateParams 动画参数
     * @param tranType        动画类型
     */
    private static void translateAsy(TranslateParams translateParams, String tranType) {
        float start = 0F;
        float end = 360F;
        View view = null;
        int duration = 1000;
        Animator.AnimatorListener animationListener = null;
        if (translateParams != null) {
            if (TextUtils.equals(TRANSLATION_Y, tranType)) {
                if (translateParams.getStartY() != start) {
                    start = translateParams.getStartY();
                }
                if (translateParams.getEndY() != end) {
                    end = translateParams.getEndY();
                }
            } else if (TextUtils.equals(TRANSLATION_X, tranType)) {
                if (translateParams.getStartX() != start) {
                    start = translateParams.getStartX();
                }
                if (translateParams.getEndY() != end) {
                    end = translateParams.getEndY();
                }
            }

            if (translateParams.getView() != null) {
                view = translateParams.getView();
            }
            if (translateParams.getTranslateAnimationListener() != null) {
                animationListener = translateParams.getTranslateAnimationListener();
            }
        }
        if (view != null) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(view, tranType, start, end);
            animator.setDuration(1000).start();
            if (animationListener != null) {
                animator.addListener(animationListener);
            }
            animator.setDuration(duration).start();
        } else {
            Log.v(TAG, PROMPT);
        }
    }

    /**透明度变化的动画
     * @param params 动画参数
     */
    public static void alptaAsy(AlptaParams params) {
        if (params != null) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(params.getView(), ALPHA, params.getStartAlpha(), params.getEndAlpha());
            if (params.getAlphaAnimationListener()!=null){

                animator.addListener(params.getAlphaAnimationListener());
            }
            animator.setDuration(params.getDuration());
            animator.start();
        }

    }

    /**
     * 旋转动画的方法
     * @param params 动画参数
     */
    public static ObjectAnimator rotationAsy(RotationParams params) {
        if (params != null) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(params.getView(), ROTATION, params.getStartDegrees(), params.getEndDegrees());
            if (params.getRotationAnimationListener()!=null){

                animator.addListener(params.getRotationAnimationListener());
            }
            animator.setRepeatCount(params.getRepeatCount());
            animator.setDuration(params.getDuration());
            animator.start();
            return animator;
        }
        return null;
    }
}
